package cn.nbt.busManage.controller;

import cn.nbt.busManage.common.ResponseResult;
import cn.nbt.busManage.model.dto.BusDto;
import cn.nbt.busManage.model.dto.RouteDto;
import cn.nbt.busManage.model.vo.BusVo;
import cn.nbt.busManage.service.BusService;
import cn.nbt.busManage.service.PointService;
import cn.nbt.busManage.service.RouteService;
import org.hibernate.usertype.BaseUserTypeSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.objenesis.ObjenesisBase;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/bus")
public class BusController {

    @Autowired
    private BusService busService;

    @Autowired
    private RouteService routeService;

    /**
     * 查询所有公交车信息
     * @return 公交车信息
     */
    @GetMapping("/list")
    public ResponseResult<Object> findAll(){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<BusDto> all = busService.findAll();
            ArrayList<BusVo> busVos = new ArrayList<>();
            for(BusDto busDto : all){
                busVos.add(new BusVo(busDto));
            }
            result.Success("查询成功",busVos);
        }catch (Exception e){
            result.Error("失败啦！",e.getMessage());
        }
        return result;
    }

    /**
     * 根据id查询公交车信息
     * @param id 公交车id
     * @return 公交车信息
     */
    @GetMapping("/findById")
    public ResponseResult<Object> findById(@RequestParam("id") Integer id){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            BusDto busDto = busService.findById(id);
            BusVo busVo = new BusVo(busDto);
            result.Success("查询成功",busVo);
        }catch (Exception e){
            result.Error("失败啦！",e.getMessage());
        }
        return result;
    }

    /**
     * 更新公交车信息
     * @param busVo 公交车信息
     * @return 返回结果
     */
    @PutMapping("/update")
    public ResponseResult<Object> update(@RequestBody BusVo busVo){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            String licensePlate = busVo.getLicensePlate();
            BusDto byId = busService.findById(busVo.getId());
            if(!licensePlate.equals(byId.getLicensePlate())){
                BusDto byLicensePlate = busService.findByLicensePlate(licensePlate);
                if(byLicensePlate != null){
                    result.BadRequest("失败啦！","车牌号已存在");
                    return result;
                }
            }
            BusDto busDto = new BusDto(busVo);
            busService.update(busDto);
            result.Success("更新成功");
        }catch (Exception e){
            result.Error("失败啦！",e.getMessage());
        }
        return result;
    }

    /**
     * 根据站点查询公交车
     * @param pointName 站点
     * @return 公交车信息
     */
    @GetMapping("/findByPoint")
    public ResponseResult<Object> findByPoint(@RequestParam("pointName") String pointName){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<BusDto> byPoint = busService.findByPoint(pointName);
            ArrayList<BusVo> busVos = new ArrayList<>();
            for(BusDto busDto : byPoint){
                busVos.add(new BusVo(busDto));
            }
            result.Success("查询成功",busVos);
        }catch (Exception e){
            result.Error("失败啦！",e.getMessage());
        }
        return result;
    }

    /**
     * 根据车牌号查询车辆
     * @param licensePlate 车牌号
     * @return 车辆信息
     */
    @GetMapping("/findByLicensePlate")
    public ResponseResult<Object> findByLicensePlate(@RequestParam("licensePlate") String licensePlate){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            BusDto byLicensePlate = busService.findByLicensePlate(licensePlate);
            BusVo busVo = new BusVo(byLicensePlate);
            result.Success("查询成功",busVo);
        }catch (Exception e){
            result.Error("失败啦！",e.getMessage());
        }
        return result;
    }

    /**
     * 添加公交车信息
     * @param busVo 公交车信息
     * @return 返回结果
     */
    @PostMapping("/save")
    public ResponseResult<Object> save(@RequestBody BusVo busVo){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            BusDto busDto = new BusDto(busVo);
            if(busService.ifFindByLicensePlate(busDto.getLicensePlate())){
                result.BadRequest("失败啦！","车牌号已存在");
                return result;
            }
            busService.save(busDto);
            result.Success("保存成功");
        }catch (Exception e){
            result.Error("失败啦！",e.getMessage());
        }
        return result;
    }

    /**
     * 删除公交车信息
     * @param id 公交车id
     * @return 返回结果
     */
    @DeleteMapping("/delete")
    public ResponseResult<Object> deleteById(@RequestParam("id") int id){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            busService.delete(id);
            result.Success("删除公交车成功");
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }
}
